﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class LangController : Controller
    {
        //
        // GET: /Lang/
        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSTrinhDoNgoaiNgu
                            from cap in context.DSCapNgoaiNgu
                            where dt.IDCanBo == id && dt.IDTrinhDo == cap.ID
                            select new { dt.ID, dt.STT, dt.Ten, TenTrinhDo = cap.Ten, dt.NgayCap, dt.IsThongKe }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.STT,
                                 customer.Ten,
                                 customer.TenTrinhDo,
                                 customer.NgayCap,
                                 
                                 customer.IsThongKe
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id)
        {
            List<TrinhDoNgoaiNgu> dsqhgd = TrinhDoNgoaiNguModels.GetTrinhDoNNTheoCanBo(id);
            ViewBag.TrinhDoNgoaiNgu = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {
            ViewBag.Id = id;
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = from th in context.DSCapNgoaiNgu
                     select th;
            List<SelectListItem> dscnn = new List<SelectListItem>();
            foreach (var item in kq)
            {
                SelectListItem slitem = new SelectListItem();
                slitem.Value = item.ID.ToString();
                slitem.Text = item.Ten;
                dscnn.Add(slitem);
            }
            ViewBag.Dscnn = dscnn;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoNgoaiNgu object1 = new TrinhDoNgoaiNgu();
            object1.STT = Convert.ToInt32(form["STT"]);
            object1.Ten = form["Ten"];
            object1.IDTrinhDo = Convert.ToInt32(form["TrinhDo"]);

            bool isChecked = (form["IsThongKe"] != "false");
            object1.IsThongKe = isChecked;
            object1.NgayCap = DateTime.Parse(form["NgayCap"],ci.DateTimeFormat);

            object1.IDCanBo = id;

            TrinhDoNgoaiNguModels.Add(object1);
            //return RedirectToAction("Index");
            return RedirectToAction("Index", "Lang", new { id = object1.IDCanBo });
        }
        public ActionResult Update(int id)
        {           
            TrinhDoNgoaiNgu listQHGD = TrinhDoNgoaiNguModels.GetChiTietTrinhDoNgoaiNgu(id);
            ViewBag.TrinhDoNgoaiNgu = listQHGD;
            // List trinh do ngoai ngu
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var kq = from th in context.DSCapNgoaiNgu
                     select th;                  
            ViewBag.Dscnn = kq;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoNgoaiNgu object1 = TrinhDoNgoaiNguModels.GetChiTietTrinhDoNgoaiNgu(id);
            object1.STT = Convert.ToInt32(form["STT"]);
            object1.Ten = form["Ten"];
            object1.IDTrinhDo =Convert.ToInt32(form["TrinhDo"]);
            bool isChecked = (form["IsThongKe"] != "false");
            object1.IsThongKe = isChecked;
            object1.NgayCap = DateTime.Parse(form["NgayCap"],ci.DateTimeFormat);


            TrinhDoNgoaiNguModels.Update(object1);
            return RedirectToAction("Index", "Lang", new { id = object1.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            TrinhDoNgoaiNgu s = TrinhDoNgoaiNguModels.GetChiTietTrinhDoNgoaiNgu(id);
            TrinhDoNgoaiNguModels.Delete(id);
            return RedirectToAction("Index", "Lang", new { id = s.IDCanBo });
        }
    }
}
